<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Classified extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
			$this->load->library('session');
		}
		
		function index($query_id=0, $order_by='start_date', $sort_order='asc', $current_page=0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					}
				}
				
			$results_per_page = 3;
		
		
			$url = CURL_URL;
			$data = array('www-command'=>'classified-all_classified', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page, 'order_by'=>$order_by, 'sort_order'=>$sort_order,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			if($response->message !== 'No Classifieds'){
				$data['classified_all_classified'] = $response;
				
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
			
				$this->load->library('pagination');			
				$config['base_url'] = base_url().'admin/classified/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
				$config['per_page'] = $results_per_page;
				$config['total_rows']  = $response->total;
				$config['uri_segment'] = 7;
				$config['num_links'] = 1;
				$config['prev_link'] = 'Previous';
				$config['next_link'] = 'Next';
				$config['first_link'] = 'First';
				$config['last_link'] = 'Last';
				$this->pagination->initialize($config);
				$data['pagination'] = $this->pagination->create_links();
				$data['error'] = '';
			}else{
				$data['error'] = 1;
				$data['classified_all_classified'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['classified_fieds'] = array('category_name'=>'Category', 'start_date'=>'Start Date', 'description'=>'Description', 'email'=>'Email', 'user_name'=>'Posted By', 'status'=>'Status', 'created_date'=>'Date');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			$data['query_id'] = $query_id;
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			$this->load->view('admin/classified', $data);
 		}
		
		function search_by(){
			if($this->input->post('search') == ''){
				$search = $this->input->post('category_date');
			}else{
				$search = $this->input->post('search');	
			}
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/classified/index/'.$query_id);
			
		}	
		
		/* function sub_cat_list(){
			//echo 'hi';exit;
				//print_r($_POST['id']);exit;
			$url = CURL_URL;
			
			$data = array('www-command'=>'classified-get_subcategory_list','category_id'=>$this->uri->segment(4));
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			if(response->message == 'Submodule by id is failed'){
				$data['sub_err'] = 1;
			}
			$data['sub_cat_list'] = $response;
			
		} */
		
		function create_classified(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('category','Category','callback_cat');
				$this->form_validation->set_rules('classified','Classified','callback_type');
				$this->form_validation->set_rules('start','Start Date','required');
				$this->form_validation->set_rules('description','Description','max_length[500]');
				$this->form_validation->set_rules('meta_description','Meta description','max_length[500]');
				$this->form_validation->set_rules('meta_key','Meta Key','max_length[500]');
				$this->form_validation->set_rules('contact','Contact Details','required');
				$this->form_validation->set_rules('email','Email','valid_email');
				$this->form_validation->set_rules('phone','Phone No','numeric|max_length[10]');
				$this->form_validation->set_rules('mobile','Mobile','numeric|max_length[10]');
				$this->form_validation->set_rules('url','Url','callback_valid_url_format');
			if($this->form_validation->run() == false){
					$url = CURL_URL;
					$data = array('www-command'=>'classified-get_category_list');
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					$category_list = $response;			
					//echo '<pre>';print_r($category_list);exit;
					curl_close($ch);			
					
					$data = array('www-command'=>'classified-get_subcategory_list','category_id'=>$this->uri->segment(4));
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					//$data['sub'] = 1;
					if($response->message == 'Submodule by id is failed'){
						$data['sub'] = 1;
					}else{
						$data['sub'] = '';
					}
					$data['sub_cat_list'] = $response;
					//echo '<pre>';print_r($data);exit;
					$data['category_list'] = $category_list;
					$data['category_type'] = array('premium'=>'Primium','standard'=>'Standard');
					$data['save_upload'] = '';
					//echo '<pre>';print_r($data);exit;
					$this->load->view('admin/create_classified_page', $data);
			}else{
				
				if(isset($_POST['save_upload'])){
					$this->session->set_userdata($_POST);
					redirect('admin/classified/classified_upload');
				}else{
					
					$url = CURL_URL;
					$data = array('www-command'=>'classified-create_classified',
							  'category_id'=>$this->input->post('category'),
							  'subcategory_id'=>$this->input->post('subcategory'),
							  'classified_type'=>$this->input->post('classified'),
							  'start_date'=>$this->input->post('start'),
							  'description'=>$this->input->post('description'),
							  'meta_description'=>$this->input->post('meta_description'),
							  'meta_keywords'=>$this->input->post('meta_key'),
							  'contact_details'=>$this->input->post('contact'),
							  'phone_number'=>$this->input->post('phone'),
							  'mobile_number'=>$this->input->post('mobile'),
							  'email'=>$this->input->post('email'),
							  'classified_url'=>$this->input->post('url'),
							);
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));		
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/classified');
				}
			}
		}
		
		function classified_upload(){
			$this->load->view('admin/create_classified_upload');
		}
		
		function create_classified_upload(){
			$files = array('tmp_name'=>$_FILES['image']['tmp_name'],'name'=>$_FILES['image']['name']);
					$url = CURL_URL;
					//echo '<pre>';print_r($_FILES);exit;
					$data = array('www-command'=>'classified-create_classified',
							  'category_id'=>$this->session->userdata('category'),
							  'subcategory_id'=>$this->session->userdata('subcategory'),
							  'classified_type'=>$this->session->userdata('classified'),
							  'start_date'=>$this->session->userdata('start'),
							  'description'=>$this->session->userdata('description'),
							  'meta_description'=>$this->session->userdata('meta_description'),
							  'meta_keywords'=>$this->session->userdata('meta_key'),
							  'contact_details'=>$this->session->userdata('contact'),
							  'phone_number'=>$this->session->userdata('phone'),
							  'mobile_number'=>$this->session->userdata('mobile'),
							  'email'=>$this->session->userdata('email'),
							  'classified_url'=>$this->session->userdata('url'),
							  'classified_image'=>$files
							);
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					$category_list = $response;		
					//echo '<pre>';print_r($category_list);exit;
					curl_close($ch);
					redirect('admin/classified');
		}
		
		function cat($sel){
			if($sel == ''){
				$this->form_validation->set_message('cat','Please select a category');
				return false;
			}else{
				return true;
			}
		}
		function type($sel){
			if($sel == ''){
				$this->form_validation->set_message('type','Please select a type');
				return false;
			}else{
				return true;
			}
		}
		
		function valid_url_format($url){
				if($url == ''){
					return true;
				}
			$pattern = '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
			if(!preg_match($pattern, $url)){
				$this->form_validation->set_message('valid_url_format', 'The URL you entered is not accessible.');
				return false;
				}
				return true;
		}
		
		function edit_classified(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('category','Category','callback_cat');
				$this->form_validation->set_rules('classified','Classified','callback_type');
				$this->form_validation->set_rules('start','Start Date','required');
				$this->form_validation->set_rules('description','Description','max_length[500]');
				$this->form_validation->set_rules('meta_description','Meta description','max_length[500]');
				$this->form_validation->set_rules('meta_key','Meta Key','max_length[500]');
				$this->form_validation->set_rules('contact','Contact Details','required');
				$this->form_validation->set_rules('email','Email','valid_email');
				$this->form_validation->set_rules('phone','Phone No','numeric|max_length[10]');
				$this->form_validation->set_rules('mobile','Mobile','numeric|max_length[10]');
				$this->form_validation->set_rules('url','Url','callback_valid_url_format');
			if($this->form_validation->run() == false){
					$url = CURL_URL;
					
					if($this->uri->segment(4) > 0){
						$_SESSION['classified_id'] = $this->uri->segment(4);
					}
					$data = array('www-command'=>'classified-get_classified','classified_id'=>$_SESSION['classified_id']);
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					$classified_get_classified = $response;			
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					
					$data = array('www-command'=>'classified-get_category_list');
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					$category_list = $response;			
					//echo '<pre>';print_r($category_list);exit;
					curl_close($ch);		
					
					
					if($this->uri->segment(5) > 0){
						$_SESSION['category_id'] = $this->uri->segment(5);
					}
					$data = array('www-command'=>'classified-get_subcategory_list','category_id'=>$_SESSION['category_id']);
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					//$data['sub'] = 1;
					if($response->message == 'Submodule by id is failed'){
						$data['sub'] = 1;
					}else{
						$data['sub'] = '';
					}
					$data['sub_cat_list'] = $response;
					//echo '<pre>';print_r($data);exit;
					$data['category_list'] = $category_list;
					$data['classified_get_classified'] = $classified_get_classified;
					$data['category_type'] = array('premium'=>'Primium','standard'=>'Standard');
					$data['save_upload'] = '';
					//echo '<pre>';print_r($data);exit;
					$this->load->view('admin/edit_classified_page', $data);
			}else{
				
				if(isset($_POST['save_upload'])){
					$this->session->set_userdata($_POST);
					$edit_id = rand();
					if($this->input->post('checkbox') == 'on'){
						$_SESSION['status'] = 1;
					}else{
						$_SESSION['status'] = 0;
					}
					//echo '<pre>';print_r($this->session->userdata('checkbox'));exit;
					redirect('admin/classified/classified_upload/'.$edit_id);
				}else{
					if($this->input->post('checkbox') == 'on'){
						$status = 1;
					}else{
						$status = 0;
					}
					
					$url = CURL_URL;
					$data = array('www-command'=>'classified-edit_classified',
							  'category_id'=>$this->input->post('category'),
							  'subcategory_id'=>$this->input->post('subcategory'),
							  'classified_type'=>$this->input->post('classified'),
							  'start_date'=>$this->input->post('start'),
							  'description'=>$this->input->post('description'),
							  'meta_description'=>$this->input->post('meta_description'),
							  'meta_keywords'=>$this->input->post('meta_key'),
							  'contact_details'=>$this->input->post('contact'),
							  'phone_number'=>$this->input->post('phone'),
							  'mobile_number'=>$this->input->post('mobile'),
							  'email'=>$this->input->post('email'),
							  'classified_url'=>$this->input->post('url'),
							  'status'=>$status,
							  'classified_id'=>$_SESSION['classified_id']
							);
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));		
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/classified');
				}
			}
		}
		
		function edit_classified_upload(){
			$files = array('tmp_name'=>$_FILES['image']['tmp_name'],'name'=>$_FILES['image']['name']);
					$url = CURL_URL;
					//echo '<pre>';print_r($_FILES);exit;
					$data = array('www-command'=>'classified-edit_classified',
							  'category_id'=>$this->session->userdata('category'),
							  'subcategory_id'=>$this->session->userdata('subcategory'),
							  'classified_type'=>$this->session->userdata('classified'),
							  'start_date'=>$this->session->userdata('start'),
							  'description'=>$this->session->userdata('description'),
							  'meta_description'=>$this->session->userdata('meta_description'),
							  'meta_keywords'=>$this->session->userdata('meta_key'),
							  'contact_details'=>$this->session->userdata('contact'),
							  'phone_number'=>$this->session->userdata('phone'),
							  'mobile_number'=>$this->session->userdata('mobile'),
							  'email'=>$this->session->userdata('email'),
							  'classified_url'=>$this->session->userdata('url'),
							  'classified_image'=>$files,
							  'classified_id'=>$_SESSION['classified_id'],
							  'status'=>$_SESSION['status']
							);
					$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					$category_list = $response;		
					//echo '<pre>';print_r($category_list);exit;
					curl_close($ch);
					redirect('admin/classified');
		}
}